package Views;

import UCM.UCDeleteUser;
import UCM.UCDisplayDeleteUser;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

/**
 * Creates the delete user interface
 * @author Josh
 */
public class ViewDeleteUser extends JPanel {

    private static final int maxChar = 10;
    private final UCDisplayDeleteUser UCDisplayDeleteUser = new UCDisplayDeleteUser(this);
    private final UCDeleteUser UCDeleteUser = new UCDeleteUser(this);
    private MainUI mainView;
    private JButton deleteButton = new JButton("Delete User");
    private JPanel enterPasswordPanel = new JPanel();
    private JPanel passwordPanel = new JPanel();
    private JLabel passwordLabel = new JLabel("Enter password: ");
    private JPasswordField passwordField = new JPasswordField(maxChar);
    private JPanel passwordConfirmPanel = new JPanel();
    private JButton deleteUserButton = new JButton("Delete");
    private JPanel deleteFailurePanel = new JPanel();
    private JLabel deleteFailureLabel = new JLabel();
    private JPanel deleteSuccessPanel = new JPanel();
    private JLabel deleteSuccessLabel = new JLabel("You have successfully deleted your account.");

    /**
     * Construction that initualises the delete user panel
     * @param mainView
     */
    public ViewDeleteUser(MainUI mainView) {
        this.mainView = mainView;
        this.add(deleteButton);
        deleteButton.addActionListener(new AdapterUCCToAL(UCDisplayDeleteUser));

        enterPasswordPanel.add(passwordPanel);
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);
        enterPasswordPanel.add(passwordConfirmPanel);
        enterPasswordPanel.add(deleteUserButton);
        deleteUserButton.addActionListener(new AdapterUCCToAL(UCDeleteUser));
        passwordField.setEchoChar('*');

        deleteFailurePanel.add(deleteFailureLabel);

        deleteSuccessPanel.add(deleteSuccessLabel);

        this.setVisible(true);
    }

    /**
     * Shows the delete user panel
     */
    public void showDeleteUserPanel() {
        mainView.removeCenterPanel();
        mainView.add(enterPasswordPanel, BorderLayout.CENTER);
        mainView.pack();
        enterPasswordPanel.setVisible(true);
    }

    /**
     * if the delete fails this function will inform the user why
     * @param text
     */
    public void updateFailure(String text) {
        deleteFailureLabel.setText(text);
        mainView.removeCenterPanel();
        mainView.add(deleteFailurePanel, BorderLayout.CENTER);
        mainView.pack();
    }

    /**
     * Informs the user of success
     */
    public void updateSuccess() {
        mainView.removeCenterPanel();
        mainView.add(deleteSuccessPanel, BorderLayout.CENTER);
        mainView.pack();
    }

    /**
     * gets the password field
     * @return
     */
    public char[] getPasswordField() {
        return passwordField.getPassword();
    }
}
